﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace rssilocalizer
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private List<string> r_files = new List<string>();
        private class RSSI_Location
        {
            double x;
            public double X
            {
                get { return x; }
                set { x = value; }
            }

            double y;
            public double Y
            {
                get { return y; }
                set { y = value; }
            }

            Int64 start_timestamp;
            public Int64 Start_timestamp
            {
                get { return start_timestamp; }
                set { start_timestamp = value; }
            }

            Int64 end_timestamp;
            public Int64 End_timestamp
            {
                get { return end_timestamp; }
                set { end_timestamp = value; }
            }

            public List<List<int>> rssi_values;
        }

        List<RSSI_Location> rls = new List<RSSI_Location>();
        private void button1_Click(object sender, EventArgs e)
        {  
            string odom = File.ReadAllText("odom.txt");
            string[] lines = odom.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            RSSI_Location rl = new RSSI_Location();
            int count_x = 0;
            double last_x = 0;
            Int64 last_timestamp = 0;

            foreach (string line in lines)
            {
                string line2 = line.Trim();
                string[] fields = line2.Split(new char[] { ',' });
                Int64 timestamp = Int64.Parse(fields[0]);
                double x = double.Parse(fields[1]);
                double y = double.Parse(fields[2]);

                if (last_x.Equals(x))
                {
                    if (count_x.Equals(0))
                    {
                        rl = new RSSI_Location();
                        rl.rssi_values = new List<List<int>>();
                        rl.Start_timestamp = last_timestamp;
                        rl.X = last_x;
                        rl.Y = y;
                    }
                    count_x++;
                }
                else if (count_x > 0)
                {
                    rl.End_timestamp = last_timestamp;
                    rls.Add(rl);
                    count_x = 0;
                }

                last_x = x;
                last_timestamp = timestamp;
            }

            Add_RSSI();

            Print();
        }

        private void Add_RSSI()
        {
            r_files.Add("sigstr_20080302_external0_r01.log");
            r_files.Add("sigstr_20080302_external1_r01.log");
            r_files.Add("sigstr_20080302_external0_r02.log");
            r_files.Add("sigstr_20080302_external1_r02.log");
            r_files.Add("sigstr_20080302_external0_r03.log");
            r_files.Add("sigstr_20080302_external1_r03.log");
            r_files.Add("sigstr_20080302_external0_r04.log");
            r_files.Add("sigstr_20080302_external1_r04.log");

            Int64 timestamp;

            foreach (RSSI_Location rl in rls)
            {
                foreach (string r_file in r_files)
                {
                    string file = File.ReadAllText(r_file).Replace('\t', ' ');
                    string[] lines = file.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    List<int> rssis = new List<int>();
                    foreach (string line in lines)
                    {
                        string line2 = line.Trim();
                        string[] fields = line2.Split(new char[] { ' ' });
                        timestamp = Int64.Parse(fields[0]);
                        if (timestamp >= rl.Start_timestamp &&
                            timestamp <= rl.End_timestamp)
                        {
                            rssis.Add(int.Parse(fields[2]));
                        }

                    }
                    rl.rssi_values.Add(rssis);
                }
            }

        }

        int receiver_counter;
        private void Print()
        {
            File.Delete("targets.txt");
            File.Delete("inputs.txt");
            int index = 0;
            int last_index = 0;            
            bool first;
            List<Output> outputs = new List<Output>();
            foreach (RSSI_Location rl in rls)
            {
                int min = int.MaxValue;
                foreach (List<int> rssis in rl.rssi_values)
                {
                    if (rssis.Count < min)
                        min = rssis.Count;
                }

                first = true;
                receiver_counter = -1;
                if (min > 0)
                {
                    foreach (List<int> rssis in rl.rssi_values)
                    {
                        receiver_counter++;
                        if (first)
                        {
                            last_index = index;
                            Output o = new Output();
                            for (int j = 0; j < min; j++)
                            {
                                o.x = rl.X;
                                o.y = rl.Y;
                                o.router_rssis = new List<int>(9);
                                outputs.Add(o);
                                index++;
                            }
                            first = false;
                        }
                        int i = 0;
                        for (int k = last_index; k < index; k++)
                        {
                            outputs[k].router_rssis.Insert(receiver_counter, rssis[i]);
                            i++;
                        }
                    }


                }
            }
            int max_reciver_count = r_files.Count;
            first = true;
            bool second = false;
            string targets = string.Empty;
            string inputs = string.Empty;

            for (int x = 0; x < max_reciver_count; x++)
            {
                foreach (Output o1 in outputs)
                {
                    if (first)
                    {
                        targets+= o1.x.ToString() + "\t";
                        txtTargets.AppendText(o1.x.ToString() + "\t");
                    }
                    else if (second)
                    {
                        targets+= o1.y.ToString() + "\t";
                        txtTargets.AppendText(o1.y.ToString() + "\t");
                    }

                    inputs+=o1.router_rssis[x].ToString() + "\t";
                    txtInputs.AppendText(o1.router_rssis[x].ToString() + "\t");
                }
                inputs.TrimEnd(new char[] { '\t' });
                inputs+= "\r\n";

                txtInputs.Text.TrimEnd(new char[] { '\t' });
                txtInputs.AppendText("\r\n");
                if (first)
                {
                    targets.TrimEnd(new char[] { '\t' });
                    targets+= "\r\n";

                    txtTargets.Text.TrimEnd(new char[] { '\t' });
                    txtTargets.AppendText("\r\n");
                    first = false;
                    second = true;
                }
                else if (second)
                    second = false;

            }
            File.WriteAllText("inputs.txt", inputs);
            File.WriteAllText("targets.txt", targets);

            MessageBox.Show("Done!");
        }

        private class Output
        {
            public List<int> router_rssis; 
            public double x;
            public double y;
        }
    }
}
